<?php

require_once dirname(__FILE__).'/OController.php';

class AccountController extends OController
{
	public $menu = array(
		array('label' => 'Account Overview', 'url' => array('owner/account/info')),
		array('label' => 'Modify Password', 'url' => array('owner/account/update', 'fields' => 'password')),
		array('label' => 'Modify Basic Info', 'url' => array('owner/account/update','fields' => 'basic')),
		array('label' => 'Modify Payment Details', 'url' => array('owner/account/update', 'fields' => 'payment'))
	);
	
	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('info','update', 'overview'),
				'users'=>array('@'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionInfo()
	{
		$owner = Owner::model()->find('loginid=:owner_id', array(':owner_id' => Yii::app()->user->id));
		$this->render('info', array('owner' => $owner));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($fields)
	{
		$model = Owner::model()->find('loginid=:owner_id', array(':owner_id' => Yii::app()->user->id));

		// Uncomment the following line if AJAX validation is needed
		$this->performAjaxValidation($model);

		if(isset($_POST['Owner']))
		{
			$model->attributes=$_POST['Owner'];
			if($model->save())
				$this->redirect(array('owner/account/info'));
		}

		$this->render('update',array(
			'model'=>$model,
			'fields' => $fields
		));
	}


	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$this->actionOverview();
	}
	
	public function actionOverview() {
		$this->layout = '//layouts/main';
		$owner = sess('owner');
		
		//Summary Report
		$owner_id = Yii::app()->user->id;
		$date = date('Y-m-d');
		$arr = array('pagination' => array('pageSize' => 30));
    	$cond = "owner_id='$owner_id' AND adddate='$date'";
    	$sql = "SELECT 
    				adddate,
    				validclick,
    				vjoinmb,
    				check_firstordernum,
    				website
    			FROM diary_log
    			WHERE $cond
    			UNION
    			SELECT 
    				'$date' adddate,
    				sum(validclick) validclick,
    				sum(vjoinmb) vjoinmb,
    				sum(check_firstordernum) check_firstordernum,
    				'Total' website 
    			FROM diary_log
    			WHERE $cond";
    	$arr['totalItemCount'] = 4;
    	$reportData = new QpidSqlDataProvider($sql, $arr);
		
		$data = array(
			'owner' => $this->loadModel($owner['owner_id']),
			'reportData' => $reportData,
			'balanceData' => QpidBalance::getDataProvider($owner_id)
		);
		$this->render('overview', $data);
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Owner::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='owner-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
